package com.study.jpush;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.Notification;

import java.util.HashMap;
import java.util.Map;

import static cn.jpush.api.push.model.notification.PlatformNotification.ALERT;

public class JpushDemo {
    public static void main(String[] args) {
        //蜗牛
        //JPushClient jpushClient = new JPushClient("5f5ce90d7af0ef3d06b5265b", "f5eba399005f4c8d95894eca", null, ClientConfig.getInstance());
        //测试
        JPushClient jpushClient = new JPushClient("ffe3e8ca250f8664746b6f8d", "ea4eb97f219eb395134f3a24", null, ClientConfig.getInstance());

        // For push, all you need do is to build PushPayload object.
        PushPayload payload = buildPushObject_android_tag_alertWithTitle();
        //PushPayload payload = buildPushObject_all_all_alert();

        try {
            PushResult result = jpushClient.sendPush(payload);
            //result.
            jpushClient.close();

        } catch (APIConnectionException e) {
            // Connection error, should retry later

        } catch (APIRequestException e) {
            // Should review the error, and fix the request
        }
    }

    public static PushPayload buildPushObject_android_tag_alertWithTitle() {
        Map<String, String> params = new HashMap<>();
        params.put("key", "value");
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                //.setAudience(Audience.tag("vip","tag1"))//客户端包含其中一个即可收到推送
                //.setAudience(Audience.tag_and("vip","tag1"))//客户端包含所有tag才能收到推送
                //.setAudience(Audience.tag_not("vip"))//客户端不包含此tag才能收到推送
                .setAudience(
                        Audience.newBuilder()
                                .addAudienceTarget(AudienceTarget.tag("vip","tag"))
                                //.addAudienceTarget(AudienceTarget.tag("s"))//增加同种tag会覆盖上面的条件
                                //.addAudienceTarget(AudienceTarget.tag_and("n","m"))
                                //.addAudienceTarget(AudienceTarget.tag("vip"))
                                .build()
                )
                //.setAudience(Audience.tag("vip","tag1"))
                //.setAudience()
                .setNotification(Notification.android("content", "title", params))
                //.setNotification(Notification.ios()
                .build();
    }

    public static PushPayload buildPushObject_all_all_alert() {
        return PushPayload.alertAll(ALERT);
    }
}
